package com.facebook.presto.tdengine;

import com.facebook.presto.spi.*;
import com.facebook.presto.spi.connector.ConnectorSplitManager;
import com.facebook.presto.spi.connector.ConnectorTransactionHandle;

import java.util.ArrayList;
import java.util.List;

public class TdEngineSplitManager implements ConnectorSplitManager {
    private TdEngineSplitManager() {
    }

    private static TdEngineSplitManager instance;

    public static TdEngineSplitManager getInstance() {
        if (instance == null) {
            synchronized (TdEngineSplitManager.class) {
                if (instance == null) {
                    instance = new TdEngineSplitManager();
                }
            }
        }

        return instance;
    }

    @Override
    public ConnectorSplitSource getSplits(ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorTableLayoutHandle layout, SplitSchedulingContext splitSchedulingContext) {
        TdEngineTableLayoutHandle layoutHandle = (TdEngineTableLayoutHandle) layout;
        TdEngineTableHandle tableHandle = layoutHandle.getTable();
        List<ConnectorSplit> splits = new ArrayList<>();
        splits.add(new TdEngineSplit(tableHandle.getSchemaName(), tableHandle.getTableName()));
        return new FixedSplitSource(splits);
    }
}
